/*
 * Created on Jan 30, 2005
 */
package org.vectrics.recruiter.struts;

import java.util.Collection;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.vectrics.SystemException;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.config.RecruitConfig;
import org.vectrics.struts.Request;
import org.vectrics.struts.VectricsActionForm;
import org.vectrics.user.PasswordHintQuestion;

/**
 * @author MMoore
 */
public class EmailPasswordForm extends VectricsActionForm {

	private static final long serialVersionUID = -6952145478644547357L;

	private String emailAddress = null;
	private String question = null;
	private String answer = null;
	
	private transient Logger log = Logger.getLogger(EmailPasswordForm.class);
	
	public EmailPasswordForm() {
		super();
	}

	/**
	 * @return Returns the answer.
	 */
	public String getAnswer() {
		return answer;
	}
	/**
	 * @param answer The answer to set.
	 */
	public void setAnswer(String answer) {
		this.answer = answer;
	}
	
	/**
	 * @return Returns the emailAddress.
	 */
	public String getEmailAddress() {
		return emailAddress;
	}
	/**
	 * @param emailAddress The emailAddress to set.
	 */
	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}

	
	/**
	 * @return Returns the question.
	 */
	public String getQuestion() {
		return question;
	}
	/**
	 * @param question The question to set.
	 */
	public void setQuestion(String question) {
		this.question = question;
	}
	
	public ActionErrors doValidate(ActionMapping mapping, Request request) {
		ActionErrors errors = new ActionErrors();
		log.info("Input Forward = " + mapping.getInputForward().getName());
		log.info("Forward = " + mapping.getForward());
		//log.info("Input = " + mapping.getInput());
		log.info("Path = " + mapping.getPath());
		
		if (this.wasButtonPressed(request, "submit.email.address") == true) {
			if (RecruitConfig.getRecruitInstance().getAllowPasswordEmail(ThreadProperties.getDomain()) == false)
				throw new SystemException("Exception - cannot e-mail password because system option is false");
			if (this.getEmailAddress() != null) {
				if  (this.getEmailAddress().trim().length() > 0) {
					Collection people = CoreServiceLocator.getPersonService()
							.findPersonsWithEMail(ThreadProperties.getDomain(), this.getEmailAddress());
					if (people.size() == 0) {
						log.info("Validation failed - could not find person with email address");
						errors.add("emailAddress", 
									new ActionMessage("error.no.user.with.email"));
					} else if (people.size() > 1) {
						log.error("Email address used by more than one person,"
								+ " this shouldn't happen on a configuration that allows password e-mailing: " + this.getEmailAddress());
						throw new SystemException("Email address used by more than one person,"
								+ " this shouldn't happen on a configuration that allows password e-mailing.");
					}
				} else {
					log.info("Validation failed - email address not given by user");
					errors.add("emailAddress", 
							new ActionMessage("error.email.required.for.password.email"));
				}
			}
			
		} else if (this.wasButtonPressed(request, "hint.answer.given") == true) {
			if (this.getEmailAddress() != null) {
				if (this.isBlank(getAnswer())) {
					errors.add("answer", 
							new ActionMessage("error.answer.required.for.password.email"));
				} else {
					boolean isAnswerCorrect = CoreServiceLocator.getPersonService().isHintAnswerIsCorrect(ThreadProperties.getDomain(), emailAddress, getAnswer());
					if (isAnswerCorrect == false) {
						errors.add("answer", 
								new ActionMessage("error.answer.wrong.for.password.email"));
					}
				}
				PasswordHintQuestion question = CoreServiceLocator.getPersonService().findQuestionForEmailPassword(ThreadProperties.getDomain(), this.getEmailAddress());
				this.setQuestion(question.getText());
				
			} else {
				throw new SystemException("Email address not entered, this validation should not be invoked until email is entered");
			}
		}
		request.setAttribute(mapping.getAttribute(), this);
		return(errors);	
	}
	
}
